package customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by fuchaoyang on 2018/2/5.<br/>
 * description：
 */

public class DrawView extends View {
  private Drawable mDrawable;

  public DrawView(Context context) {
    super(context);
  }

  public DrawView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
  }

  public void setDrawable(Drawable drawable) {
    mDrawable = drawable;
  }

  @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    if (mDrawable != null) {
      // 必须设置边界，否则不会绘制
      mDrawable.setBounds(0, 0, w / 2, h / 2);
    }
  }

  @Override protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mDrawable != null) {
      // 将draw绘制在画布上
      mDrawable.draw(canvas);
    }
  }
}
